package ace.cmp.spring.graphql.test.test.core.repository.impl;

import ace.cmp.spring.graphql.test.test.core.model.Author;
import ace.cmp.spring.graphql.test.test.core.repository.AuthorRepository;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/8/22 17:04
 */
@Component
public class AuthorRepositoryImpl implements AuthorRepository {

  private static List<Author> authors;

  static {
    authors =
        List.of(
            Author.builder().id(1L).title("author-1").build(),
            Author.builder().id(2L).title("author-2").build());
  }

  @Override
  public List<Author> getAll() {
    return authors;
  }

  @Override
  public List<Author> getByIdIn(List<Long> ids) {
    List<Author> result = new ArrayList<>();
    for (Author book : authors) {
      if (ids.contains(book.getId())) {
        result.add(book);
      }
    }
    return result;
  }

  @Override
  public Author getById(Long id) {
    for (Author author : authors) {
      if (author.getId() == id) return author;
    }
    return null;
  }

  @Override
  public Author modify(Long id, String title) {
    Author Author = getById(id);
    if (Author != null) {
      Author.setTitle(title);
    }
    return Author;
  }
}
